﻿using FineUI;
using System;
using System.Data;
using System.Linq;
using ZHDJ.Data.Models.Box;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.JoinParty.BLL;

namespace JoinParty.admin.ApplyJoin
{
    public partial class MyApplyJoinPartyList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "MyApplyJoinPartyView";
            }
        }

        #endregion

        #region
        public DataSet DataSource
        {
            get
            {
                return PartyJoinApplycation.GetGridData(this.txtUserName.Text.Trim(), this.txtBranch.Text.Trim(), CurrentUser.LoginID, Grid1.PageIndex, Grid1.PageSize, ddlStatus.SelectedValue, IsJoinPartyAdmin);
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            this.btnAdd.OnClientClick = WApplyJoinParty.GetShowReference("~/App/JoinParty/admin/ApplyJoin/ApplyJoinPartyEdit.aspx", "新增");
            //this.btnAdd.OnClientClick = WApplyJoinParty.GetShowReference("~/App/JoinParty/admin/FlowStep/MeetingEx.aspx", "新增");

            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();

            BindGrid();
        }


        private void BindGrid()
        {
            IQueryable<User> q = DB.Users; //.Include(u => u.Dept);

            if (GetIdentityName() != "admin")
            {
                q = q.Where(u => u.Name != "admin");
            }

            //// 在查询添加之后，排序和分页之前获取总记录数
            //Grid1.RecordCount = q.Count();

            //// 排列和数据库分页
            //q = SortAndPage<User>(q, Grid1);
            DataSet ds = DataSource;
            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        #endregion

        #region Events
        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            //CheckPowerWithWindowField("CoreUserEdit", Grid1, "editField");
            //CheckPowerWithLinkButtonField("CoreUserDelete", Grid1, "deleteField");
            //CheckPowerWithWindowField("CoreUserChangePassword", Grid1, "changePasswordField");
        }

        protected void Grid1_PreRowDataBound(object sender, FineUI.GridPreRowEventArgs e)
        {
            User user = e.DataItem as User;

            // 不能删除超级管理员
            //if (user.Name == "admin")
            //{
            //    FineUI.LinkButtonField deleteField = Grid1.FindColumn("deleteField") as FineUI.LinkButtonField;
            //    deleteField.Enabled = false;
            //    deleteField.ToolTip = "不能删除超级管理员！";
            //}
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("MyApplyJoinPartyDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            //List<int> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            //DB.Users.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.Users.Remove(u));
            //DB.SaveChanges();
            //DB.Users.Delete(u => ids.Contains(u.ID));


            // 重新绑定表格
            BindGrid();
        }

        protected void btnEnableUsers_Click(object sender, EventArgs e)
        {
            SetSelectedUsersEnableStatus(true);
        }

        protected void btnDisableUsers_Click(object sender, EventArgs e)
        {
            SetSelectedUsersEnableStatus(false);
        }

        private void SetSelectedUsersEnableStatus(bool enabled)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("MyApplyJoinPartyEdit"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            //List<int> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            //DB.Users.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => u.Enabled = enabled);
            //DB.SaveChanges();
            //DB.Users.Update(u => ids.Contains(u.ID), u => new User { Enabled = enabled });

            // 重新绑定表格
            BindGrid();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            //int userID = GetSelectedDataKeyID(Grid1);
            //string userName = GetSelectedDataKey(Grid1, 1);

            //if (e.CommandName == "Delete")
            //{
            //    // 在操作之前进行权限检查
            //    if (!CheckPower("CoreUserDelete"))
            //    {
            //        CheckPowerFailWithAlert();
            //        return;
            //    }

            //    if (userName == "admin")
            //    {
            //        Alert.ShowInTop("不能删除默认的系统管理员（admin）！");
            //    }
            //    else
            //    {
            //        var T = DB.Users.FirstOrDefault(a => a.ID == userID);
            //        DB.Users.Remove(T);
            //        DB.SaveChanges();
            //        BindGrid();
            //    }
            //}
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void rblEnableStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);

            BindGrid();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}